package com.apps.ftapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.model.RecSubjectModel;
import com.apps.ftapp.util.Tools;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

/**
 * Created by lyli on 2017/10/8.
 */

public class MyErrorSubjectRecAdapter extends BaseAdapter {


    private Context context;
    private List<RecSubjectModel.DataBean> dataList;
    private LayoutInflater layoutInflater;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;


    public MyErrorSubjectRecAdapter(Context context, List dataList) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(this.context);

        //显示图片的配置  
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));

    }


    public void addRes(List<RecSubjectModel.DataBean> data) {
        if (data != null) {
            this.dataList.clear();
            this.dataList.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        if (null != dataList && dataList.size() > 0) {
            return dataList.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        RecSubjectModel.DataBean bean = dataList.get(i);
        ViewHolder viewHolder;
        if (view == null) {
            view = this.layoutInflater.inflate(R.layout.activity_my_error_subject_rec_listview_item, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.error_title.setText(Tools.delHTMLTag(bean.getStem()));
        viewHolder.error_desc.setText(bean.getCount() + "人做答  " + bean.getWcount() + "人做错");
        viewHolder.error_score.setText(bean.getWrate());


//
//        String[] typeKey = {"single_choice","choice","uncertain_choice","fill","determine","essay","material"};
//        String[] typeName = {"单选题","多选题","不定项选择题","填空题","判断题","问答题","材料题"};

        if ("single_choice".equals(Tools.isNull(bean.getType()))) {
            viewHolder.error_type.setText("单选题");
        } else if ("choice".equals(Tools.isNull(bean.getType()))) {
            viewHolder.error_type.setText("多选题");
        } else if ("uncertain_choice".equals(Tools.isNull(bean.getType()))) {
            viewHolder.error_type.setText("不定项选择题");
        } else if ("fill".equals(Tools.isNull(bean.getType()))) {
            viewHolder.error_type.setText("填空题");
        } else if ("determine".equals(Tools.isNull(bean.getType()))) {
            viewHolder.error_type.setText("判断题");
        } else if ("essay".equals(Tools.isNull(bean.getType()))) {
            viewHolder.error_type.setText("问答题");
        } else if ("material".equals(Tools.isNull(bean.getType()))) {
            viewHolder.error_type.setText("材料题");
        }


        return view;
    }


    class ViewHolder {
        TextView error_title;
        TextView error_desc;
        TextView error_score;
        TextView error_type;

        public ViewHolder(View view) {
            error_score = view.findViewById(R.id.error_score);
            error_desc = view.findViewById(R.id.error_desc);
            error_type = view.findViewById(R.id.error_type);
            error_title = view.findViewById(R.id.error_title);
        }
    }


}
